package com.fruit.api.controller.fruit;

import com.fruit.bean.dto.fruit.FruitStockOrderInfoDto;
import com.fruit.bean.entity.fruit.FruitStockInfo;
import com.fruit.bean.entity.fruit.FruitStockOrderInfo;
import com.fruit.bean.entity.system.FileInfo;
import com.fruit.bean.enumeration.fruit.FruitStockOutStatusEnum;
import com.fruit.bean.enumeration.fruit.FruitStockStatusEnum;
import com.fruit.bean.vo.front.Rets;
import com.fruit.service.fruit.FruitStockOrderService;
import com.fruit.service.fruit.FruitStockService;
import com.fruit.service.system.FileService;
import com.fruit.utils.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 水果库存订单接口（申请入库/出库）
 */
@RestController
@RequestMapping("/fruit/stock/order")
public class FruitStockOrderController {

    @Autowired
    private FruitStockOrderService fruitStockOrderService;

    @Autowired
    private FruitStockService stockService;

    @Autowired
    private FileService fileService;

    @PostMapping("/inout")
    public Object inStock(@RequestBody FruitStockOrderInfoDto stockOrderInfoDto){

        //插入库存订单信息
        return fruitStockOrderService.inOutStockOrder(stockOrderInfoDto);
    }

    /**
     * 同意库存入库
     * @param fruitStockId 库存id
     * @return
     */
    @PostMapping("/accept")
    public Object acceptStockIn(@RequestParam Long fruitStockId){
        //查询出库存信息并更新库存状态为后台同意等待签约
        FruitStockInfo fruitStockInfo = stockService.get(fruitStockId);
        fruitStockInfo.setFruitStockStatus(FruitStockStatusEnum.ACCEPTED.getCode());
        stockService.update(fruitStockInfo);
        return Rets.success();
    }

    /**
     * 拒绝库存入库
     * @param fruitStockId 库存id
     * @return
     */
    @PostMapping("/refuse")
    public Object refuseStockIn(@RequestParam Long fruitStockId){
        //查询出库存信息并更新库存状态为后台同意等待签约
        FruitStockInfo fruitStockInfo = stockService.get(fruitStockId);
        fruitStockInfo.setFruitStockStatus(FruitStockStatusEnum.REFUSED.getCode());
        stockService.update(fruitStockInfo);
        return Rets.success();
    }

    /**
     * 用户拒绝签约取消入库
     * @param stockInfo 库存信息 stockId 库存id
     * @return
     */
    @PostMapping("/cancel")
    public Object cancelStockIn(@RequestBody Map<String,Long> stockInfo){
        Long stockId = stockInfo.get("stockId");
        //查询出库存信息并更新库存状态为后台同意等待签约
        FruitStockInfo fruitStockInfo = stockService.get(stockId);
        fruitStockInfo.setFruitStockStatus(FruitStockStatusEnum.CANCELED.getCode());
        stockService.update(fruitStockInfo);
        return Rets.success();
    }

    /**
     * 用户拒绝签约取消入库
     * @param stockInfo 库存信息 stockId 库存id
     * @return
     */
    @PostMapping("/confirm")
    public Object confirmStockIn(@RequestBody Map<String,Long> stockInfo){
        Long stockId = stockInfo.get("stockId");
        //查询出库存信息并更新库存状态为后台同意等待签约
        FruitStockInfo fruitStockInfo = stockService.get(stockId);
        fruitStockInfo.setFruitStockStatus(FruitStockStatusEnum.FINISHED.getCode());
        stockService.update(fruitStockInfo);
        return Rets.success();
    }

    /**
     * 获取合同内容
     * @param stockInfo 库存id
     * @return
     */
    @PostMapping("/contract")
    public Object getContract(@RequestBody Map<String,Long> stockInfo){
        Long stockId = stockInfo.get("stockId");
        String contractValue =  stockService.getContract(stockId);
        String[] strList = contractValue.split(";");
        return Rets.success(strList);
    }

    /**
     * 导出库存订单列表excel
     * @return
     */
    @GetMapping("/export")
    public Object export(){
        List<FruitStockOrderInfoDto> fruitStockOrderInfoList = fruitStockOrderService.listStockOrder();
        FileInfo fileInfo = fileService.createExcel("templates/stockOrder.xlsx", "系统参数.xlsx",
                Maps.newHashMap("list", fruitStockOrderInfoList));
        return Rets.success(fileInfo);
    }

    /**
     * 管理端获取出库列表
     * @return
     */
    @GetMapping("/out/list")
    public Object listStockInfo(){
        return Rets.success(fruitStockOrderService.queryStockOutPage());
    }

    /**
     * 同意库存出库
     * @param fruitStockOrderId 库存订单id
     * @return
     */
    @PostMapping("/out/accept")
    public Object acceptStockOut(@RequestParam Long fruitStockOrderId){
        return fruitStockOrderService.stockOutAccept(fruitStockOrderId);
    }

    /**
     * 拒绝库存出库
     * @param fruitStockOrderId 库存订单id
     * @return
     */
    @PostMapping("/out/refuse")
    public Object refuseStockOut(@RequestParam Long fruitStockOrderId){
        // 查询出库存信息并更新库存状态为后台拒绝出库
        FruitStockOrderInfo fruitStockOrderInfo = fruitStockOrderService.get(fruitStockOrderId);
        fruitStockOrderInfo.setStockOrderStatus(FruitStockOutStatusEnum.REFUSED.getCode());
        fruitStockOrderService.update(fruitStockOrderInfo);
        return Rets.success();
    }

    /**
     * 用户获取出库信息
     * @return
     */
    @PostMapping("/out/listByUser")
    public Object getStockOutList(@RequestBody Map<String, Long> map) {
        Long userId = map.get("userId");
        // 查询出库存信息并更新库存状态为后台拒绝出库
        return Rets.success(fruitStockOrderService.listStockOrder(userId));
    }
}
